/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswing;

import javax.swing.JPasswordField;
import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreEvent;
import spagcore.SpaglCoreFont;
import spagcore.SpaglCorePasswordField;

/**
 * A JPasswordField (javax.swing.JPasswordField) that conforms to the SpaglCorePasswordField interface
 * @author Sam Pottinger
 *
 */
public class SpaglPasswordField extends JPasswordField implements SpaglCorePasswordField {

	private static final long serialVersionUID = 1L;
	private SpaglCoreContainer parentWidget;
	private SpaglCoreFont widgetFont;
	
	/**
	 * Creates a bare-bones password field
	 * @param newParent The container that will hold this widget
	 */
	public SpaglPasswordField(SpaglCoreContainer newParent)
	{
		super();
		parentWidget = newParent;
	}
	
	/**
	 * Creates a password field with a specified length
	 * @param newParent The container that will hold this widget
	 * @param width The integer width of this password field
	 */
	public SpaglPasswordField(SpaglCoreContainer newParent, int width)
	{
		super(width);
		parentWidget = newParent;
	}
	
	/**
	 * Creates a password field with a specified length and placement parameters
	 * @param newParent The container that will hold this widget
	 * @param width The integer width of this widget
	 * @param parameters The string parameters that will be used with the layout manager
	 */
	public SpaglPasswordField(SpaglCoreContainer newParent, int width, String parameters)
	{
		super(width);
		parentWidget = newParent;
		parentWidget.placeWidget(this, parameters);
	}
	
	/**
	 * Gets the container that is holding this widget
	 * @return The parent container of this object as a SpaglCoreContainer
	 */
	public SpaglCoreContainer getContainer() {
		return parentWidget;
	}

	/**
	 * Gets the font associated with this widget
	 * @return The font that is attached with this widget as a SpaglCoreFont
	 */
	public SpaglCoreFont getWidgetFont() {
		return widgetFont;
	}

	/**
	 * Sets the font to be used by this widget
	 * @param newFont The {@link SpaglCoreFont}-compatable font used by this widget
	 */
	public void setFont(SpaglCoreFont newFont) {
		widgetFont = newFont;
		super.setFont((SpaglFont)newFont);
	}
	
	
	/**
	 * Sets the font to be used by this widget
	 * @param newFont The {@link SpaglCoreFont}-compatable font used by this widget
	 * @param dispose True if the font should be disposed of immediately (experimental)
	 */
	public void setFont(SpaglCoreFont newFont, boolean dispose) {
		if(!dispose)
			widgetFont = newFont;
		else
			widgetFont = null;
		super.setFont((SpaglFont)newFont);
	}
	
	/**
	 * Swing does not need manual disposal, so this is here just for cross library purposes
	 */ 
	public void dispose() {}
	
	/**
	 * Adds an event listener to this list
	 * 
	 * @param eventID The ID of the event to be listened for
	 * @param method The (string) name of the method to be called when the event is fired
	 * @param targetObject The object where the method should be fired
	 */
	public void addEventListener(int eventID, String method, Object targetObject) {
		try{
			switch(eventID)
			{
				case SpaglEvent.TEXT_CHANGED_EVENT:{this.getDocument().addDocumentListener(new SpaglEventListener(targetObject.getClass().getMethod(method, SpaglCoreEvent.class), targetObject, SpaglEvent.TEXT_CHANGED_EVENT)); break;}
				default: throw new IllegalArgumentException("Event not supported by this widget");
			}}
		catch (SecurityException e) {e.printStackTrace();} 
		catch (NoSuchMethodException e) {e.printStackTrace();}
		
	}
	
}
